package com.uzai.console.web.alarm;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.alarm.alarmcontacter.AlarmContacterIdDto;
import com.uzai.console.dto.alarm.alarmcontacter.AlarmContacterInfoDto;
import com.uzai.console.dto.alarm.alarmcontacter.AlarmContacterOpenidDto;
import com.uzai.console.dto.alarm.alarmcontacter.AlarmContacterQuery;
import com.uzai.console.entity.Merchant;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.service.alarm.AlarmContacterService;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.vo.alarm.alarmcontacter.AlarmContacterDingdingUrlVo;
import com.uzai.console.vo.alarm.alarmcontacter.AlarmContacterInfoVo;
import com.uzai.console.vo.alarm.alarmcontacter.AlarmContacterOpenidUrlVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 报警联系人
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "报警联系人",tags = "云监控|报警联系人")
@ApiSort(19)
@RestController
public class AlarmContacterController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(AlarmContacterController.class);

    @Autowired
    private AlarmContacterService alarmContacterService;
    @Autowired
    private MerchantService merchantService;

    @ApiOperation(value = "分页查询报警联系人列表", notes = "分页查询报警联系人列表", response = AlarmContacterInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/alarmContacter/findAlarmContacterList")
    public Object findAlarmContacterList(@RequestBody AlarmContacterQuery alarmContacterQuery){
        return alarmContacterService.findAlarmContacterList(alarmContacterQuery);
    }

    @ApiOperation(value = "查询所有报警联系人列表", notes = "查询所有报警联系人列表", response = AlarmContacterInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/alarmContacter/findAlarmContacterListSelect")
    public Object findAlarmContacterListSelect(){
        AlarmContacterQuery alarmContacterQuery = new AlarmContacterQuery();
        alarmContacterQuery.setMerId(getLoginInfo().getMerId());
        return alarmContacterService.findAlarmContacterListSelect(alarmContacterQuery);
    }

    @SystemLogType(model = SystemModelEnum.AlarmContacter, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加报警联系人", notes = "添加报警联系人")
    @ApiOperationSupport(order = 2)
    @PostMapping("/alarmContacter/addAlarmContacter")
    public Object addAlarmContacter(@RequestBody AlarmContacterInfoDto alarmContacterInfoDto){
        alarmContacterService.addAlarmContacter(alarmContacterInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.AlarmContacter, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "编辑报警联系人", notes = "编辑报警联系人" )
    @ApiOperationSupport(order = 3)
    @PostMapping("/alarmContacter/updateAlarmContacter")
    public Object updateAlarmContacter(@RequestBody AlarmContacterInfoDto alarmContacterInfoDto){
        alarmContacterService.updateAlarmContacter(alarmContacterInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.AlarmContacter, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除报警联系人", notes = "删除报警联系人")
    @ApiOperationSupport(order = 4)
    @PostMapping("/alarmContacter/deleteAlarmContacter")
    public Object deleteAlarmContacter(@RequestBody AlarmContacterIdDto alarmContacterIdDto){
        alarmContacterService.deleteAlarmContacter(alarmContacterIdDto);
        return true;
    }

    @ApiOperation(value = "查询报警联系人详情", notes = "查询报警联系人详情", response = AlarmContacterInfoVo.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/alarmContacter/findAlarmContacterInfo")
    public Object findAlarmContacterInfo(@RequestBody AlarmContacterIdDto alarmContacterIdDto){
        return alarmContacterService.findAlarmContacterInfo(alarmContacterIdDto);
    }

    @ApiOperation(value = "查询悠哉公众号关注二维码", notes = "查询悠哉公众号关注二维码", response = AlarmContacterOpenidUrlVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/alarmContacter/getWxpubQrCode")
    public Object getWxpubQrCode(){
        Long merId = getLoginInfo().getMerId();
        return  alarmContacterService.getWxpubQrCode(merId);
    }

    @ApiOperation(value = "查询悠哉公众号openid", notes = "查询悠哉公众号openid", response = AlarmContacterOpenidUrlVo.class)
    @ApiOperationSupport(order = 7)
    @PostMapping("/alarmContacter/getOpenid")
    public Object getOpenid(@RequestBody AlarmContacterOpenidDto alarmContacterOpenidDto){
        return  alarmContacterService.getWxpubOpenid(alarmContacterOpenidDto);
    }

    @ApiOperation(value = "查询如何查询钉钉机器人链接信息", notes = "查询如何查询钉钉机器人链接信息", response = AlarmContacterDingdingUrlVo.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/alarmContacter/getDingDingUrl")
    public Object getDingDingUrl(){
        return alarmContacterService.getDingdingUrl();
    }


    @ApiOperation(value = "检查运营商余额是否为0", notes = "检查运营商余额是否为0", response = Boolean.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/alarmContacter/checkCarrierBalance")
    public Object checkCarrierBalance() {

        Long merId = getLoginInfo().getMerId();

        Merchant merchant = merchantService.findByMerId(merId);
        if (merchant == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERCHAT_UNREG);
        }
        //判断余额大小
        Double balance = Tools.getDouble(merchant.getBalance());
        if (balance.doubleValue() < 0.1) { //不够支付
            return false;
        }
        return true;
    }
}
